<?php
class Admin_ArticlesController extends Zend_Controller_Action
{
    protected $_flashMessenger = null;

    public function init()
    {
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->initView();
    }

	function addAction()
	{
		$form = new ArticleForm();
		$this->view->form = $form;

		$pairs = Doctrine::getTable('Category')->fetchPairs('id', 'name');
        $categorySelect = $form->getElement('category_id');
        $categorySelect->addMultioption('0', '---Select category---');
        $categorySelect->addMultioptions($pairs);

        $pairs = Doctrine::getTable('User')->fetchPairs('id', 'name');
        $authorSelect = $form->getElement('author_id');
        $authorSelect->addMultioption('0', '---Select user---');
        $authorSelect->addMultioptions($pairs);

        if($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if($form->isValid($formData))
            {
                $filtered_data = $form->getValues(true);

                $article = new Article();
                $article->merge($filtered_data);
                $article->save();

                $this->_flashMessenger->addMessage('Article created!');
                $this->_helper->redirector('index');
            }
            else
            {
                $form->populate($formData);
            }
        }// if isPost
	}

    function editAction()
    {
        $form = new ArticleForm();
        $form->submit->setLabel('Save');

        $this->view->form = $form;

        $pairs = Doctrine::getTable('Category')->fetchPairs('id', 'name');
        $categorySelect = $form->getElement('category_id');
        $categorySelect->addMultioption('0', '---Select category---');
        $categorySelect->addMultioptions($pairs);

        $pairs = Doctrine::getTable('User')->fetchPairs('id', 'name');
        $authorSelect = $form->getElement('author_id');
        $authorSelect->addMultioption('0', '---Select user---');
        $authorSelect->addMultioptions($pairs);
        
        $article = $this->_findRecord();

        if($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if($form->isValid($formData))
            {
                $filtered_data = $form->getValues(true);

                $article->merge($filtered_data);
                $article->save();

                $this->_flashMessenger->addMessage('Article updated!');
                $this->_helper->redirector('index');;
            }
            else
            {
                $form->populate($formData);
            }
        }
        else
        {
            $form->populate($article->toArray());
        }
    }

    function deleteAction()
    {
        $article = $this->_findRecord();

        if($this->_request->isPost())
        {
            $del = $this->_request->getPost('del');
            if ($del == 'Yes')
            {
                $article->delete();

                $this->_flashMessenger->addMessage('Article deleted!');
            }

            $this->_helper->redirector('index');
        }
        else
        {
            $this->view->article = $article;
        }
    }

	function indexAction()
	{
        $this->view->authorSelect = Doctrine::getTable('User')->createSelect(false);
        $this->view->categorySelect = Doctrine::getTable('Category')->createSelect(false);

        $this->view->flash = $this->_flashMessenger->getMessages();
	}

    function ajaxFetchArticlesAction()
    {
        $params = array(
            'offset'    => $this->_request->getParam('start', 0),
            'limit'     => $this->_request->getParam('limit', 20),
            'sort'      => $this->_request->getParam('sort', 'id'),
            'dir'       => $this->_request->getParam('dir', 'ASC'),
            'author_id'  => $this->_request->getParam('author_id', 0),
            'category_id'  => $this->_request->getParam('category_id', 0),
        );

        $rawData = Doctrine::getTable('Article')->fetchExtGridData($params);

        $this->_helper->json($rawData);
    }
    
    /**
     * Finds record in database
     * 
     * @return Doctrine_Record
     */
    protected function _findRecord()
    {
        $id = (int) $this->_request->getParam('id');
        $record = Doctrine::getTable('Article')->find($id);
        if (!$record) throw new Zend_Controller_Dispatcher_Exception();

        return $record;
    }      
    
}
?>